package com.sdb.prescription.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdb.common.annotation.Log;
import com.sdb.common.core.controller.BaseController;
import com.sdb.common.core.domain.AjaxResult;
import com.sdb.common.enums.BusinessType;
import com.sdb.prescription.domain.TdPrescription;
import com.sdb.prescription.service.ITdPrescriptionService;
import com.sdb.common.utils.poi.ExcelUtil;
import com.sdb.common.core.page.TableDataInfo;

/**
 * 处方Controller
 * 
 * @author yyh
 * @date 2025-02-06
 */
@RestController
@RequestMapping("/prescription/prescription")
public class TdPrescriptionController extends BaseController
{
    @Autowired
    private ITdPrescriptionService tdPrescriptionService;

    /**
     * 查询处方列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/list")
    public TableDataInfo list(TdPrescription tdPrescription)
    {
        startPage();
        List<TdPrescription> list = tdPrescriptionService.selectTdPrescriptionList(tdPrescription);
        return getDataTable(list);
    }
    /**
     * 查询未发药列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/listUnsent")
    public TableDataInfo listNotPay(TdPrescription tdPrescription)
    {
        startPage();
        List<TdPrescription> list = tdPrescriptionService.getUnsentPrescriptions(tdPrescription);
        return getDataTable(list);
    }

    /**
     * 导出处方列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "处方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdPrescription tdPrescription)
    {
        List<TdPrescription> list = tdPrescriptionService.selectTdPrescriptionList(tdPrescription);
        ExcelUtil<TdPrescription> util = new ExcelUtil<TdPrescription>(TdPrescription.class);
        util.exportExcel(response, list, "处方数据");
    }

    /**
     * 获取处方详细信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tdPrescriptionService.selectTdPrescriptionById(id));
    }

    /**
     * 新增处方
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "处方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdPrescription tdPrescription)
    {
        return toAjax(tdPrescriptionService.insertTdPrescription(tdPrescription));
    }

    /**
     * 修改处方
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "处方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdPrescription tdPrescription)
    {
        return toAjax(tdPrescriptionService.updateTdPrescription(tdPrescription));
    }

    /**
     * 删除处方
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "处方", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tdPrescriptionService.deleteTdPrescriptionByIds(ids));
    }

    /**
     * 保存处方详细信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "处方", businessType = BusinessType.INSERT)
    @PostMapping("/savePrescription")
    public AjaxResult savePrescription(@RequestBody List<TdPrescription> prescriptionDrugs) {
//        try {
            tdPrescriptionService.savePrescription(prescriptionDrugs);
            return AjaxResult.success("处方保存成功");
//        } catch (Exception e) {
//            return AjaxResult.error("处方保存失败: " + e.getMessage());
//        }
    }

    /**
     * 修改收费状态
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @PutMapping("/updatepayStatus")
    public AjaxResult updateChargeStatus(@RequestBody TdPrescription tdPrescription) {
        return toAjax(tdPrescriptionService.updateChargeStatus(tdPrescription));
    }

    /**
     * 卡余额支付
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @PutMapping("/payByCard")
    public AjaxResult payByCard(@RequestBody TdPrescription tdPrescription) {
        return toAjax(tdPrescriptionService.payByCard(tdPrescription));
    }

}
